﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WMS.BasicData.Clients.Dto;
using WMS.Pagination;

namespace WMS.BasicData.Clients
{
    /// <summary>
    /// 客户服务
    /// </summary>
    public class ClientAppService : ApplicationService, IClientAppService
    {
        /// <summary>
        /// 客户仓储接口
        /// </summary>
        private readonly IRepository<Client> _clientRepository;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper _mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="clientRepository">客户仓储接口</param>
        /// <param name="mapper">映射</param>
        public ClientAppService(IRepository<Client> clientRepository, IMapper mapper)
        {
            _clientRepository = clientRepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 添加重复性验证
        /// </summary>
        /// <param name="code">编码</param>
        /// <returns>返回结果</returns>
        public int AddRepeatability(string code)
        {
            var reslut=_clientRepository.GetListAsync(m=>m.Code==code).Result.Count();
            return reslut;
        }
        /// <summary>
        /// 添加客户信息
        /// </summary>
        /// <param name="clientDto">客户Dto</param>
        /// <returns>返回结果</returns>
        public async Task<int> Insert(ClientDto clientDto)
        {
            var reslut = AddRepeatability(clientDto.Code!);
            if (reslut > 0) return -1;
            Client client = _mapper.Map<Client>(clientDto);
            await _clientRepository.InsertAsync(client);
            return 1;
        }
        /// <summary>
        /// 显示客户信息
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="chineseName">中文名称</param>
        /// <returns>返回结果</returns>
        public async Task<PageResult> GetInquire(int pageIndex, int pageSize, string? chineseName)
        {
            var client = await _clientRepository.GetListAsync(m => string.IsNullOrEmpty(chineseName) || m.ChineseName!.Contains(chineseName));
            PageResult pageResult = new PageResult();
            pageResult.RowCount = client.Count();
            pageResult.PageCount = (int)Math.Ceiling(pageResult.RowCount * 1.0 / pageSize);
            pageResult.Queryable = client.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pageResult;
        }
        /// <summary>
        /// 删除客户信息
        /// </summary>
        /// <param name="ids">客户编号</param>
        /// <returns>返回结果</returns>
        public async Task<int> Deleted(string ids)
        {
            var Id= ids.Split(',');
            var reslut = 0;
            foreach (var id in Id)
            {
                await _clientRepository.DeleteAsync(m=>m.Id==Convert.ToInt32(id));
                reslut++;
            }
            return reslut > 0 ? 1 : 0;
        }
        /// <summary>
        /// 编辑客户信息
        /// </summary>
        /// <param name="clientDto">客户Dto</param>
        /// <returns>返回结果</returns>
        public async Task<int> Update(ClientDto clientDto)
        {
            Client client = _mapper.Map<Client>(clientDto);
            await _clientRepository.UpdateAsync(client);
            return 1;
        }
    }
}
